﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using AdvanceLib;
using AdvanceLib.AddIn;
using AdvanceLib.ScriptCore;
using AdvanceLib.ScriptCore.DebugCore;
using AdvanceLib.Core;
using AdvanceLib.Core.Configuration;
using AdvanceLib.Extension.XseScript.DebugPasses;

namespace AdvanceLib.Extension.XseScript
{
    public class XseExtensionProvider : IExtensionProvider
    {
        #region Constructors
        public XseExtensionProvider()
        {
            #region InstallFiles
            this.Files = new Dictionary<string, string>();
            this.Files.Add(@"data\types.xse", Properties.Resources.Types);
            this.Files.Add(@"data\commands.xse", Properties.Resources.Commands);
            this.Files.Add(@"data\constants.xse", Properties.Resources.Constants);
            #endregion

            if (this.IsInstalled)
            {
                this.Initialize();
            }
        }
        #endregion

        #region Fields
        private ScriptFormatter _formatter;
        private IDebugger _debugger;
        private ScriptParser _parser;
        #endregion

        #region Properties
        public ScriptDatabase Database { get; set; }
        public Dictionary<string, string> Files { get; set; }
        #endregion

        #region Methods
        public void Initialize()
        {
            this.Database = new ScriptDatabase(this.Files.Keys);

            this._formatter = new ScriptFormatter(this);
            this._debugger = new Debugger()
            {
                DebugPasses = new List<IDebugPass>()
                {
                    new EmptyInputPass(),
                    new ParameterValidationPass(),
                    new CommandExistsPass(this),
                    new ParameterCountPass(this),
                    new ParameterTypePass(this)
                }
            };
            this._parser = new ScriptParser(this);
        }
        #endregion

        #region IExtentionProvider Member
        public IFormatter Formatter
        {
            get { return this._formatter; }
        }
        public IDebugger Debugger
        {
            get { return this._debugger; }
        }
        public IParser Parser
        {
            get { return this._parser; }
        }
        public IDecompiler Decompiler
        {
            get { throw new NotImplementedException(); }
        }
        public ScriptHost Host { get; set; }
        public Configuration Configuration { get; set; }

        #region Install/UnInstall
        public bool IsInstalled
        {
            get
            {
                return FileManager.Instance.Exists(this.Files.Keys);          
            }
        }
        public void Install()
        {
            FileManager.Instance.Create(this.Files);
            this.Initialize();
        }
        public void UnInstall()
        {
            FileManager.Instance.Delete(this.Files.Keys);
        }
        #endregion
        
        #endregion

        #region IClassInfo Member
        public string Name
        {
            get { return "XseScript"; }
        }
        public string Author
        {
            get { return "Krissel095, haefele"; }
        }
        public Version Version
        {
            get { return new Version("1.0.0.0"); }
        }
        public string Description
        { 
            get { return "XSE Extension"; } 
        }
        public Bitmap Icon 
        {
            get { return null; } 
        }
        #endregion
    }
}
